//spinBoxColumn  horizontalSlider spinBoxRow verticalSlider btnReset tableWidget lineEdit lineEdit_1 QWidgetItem
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QSlider>
#include <QtWidgets/QPushButton>
#include <QApplication>
#include <QMainWindow>
#include "StructureElementSet.h"
#include "ui_DialogStructureElement.h"

QTableWidget *tableWidget;
QSpinBox *spinBoxColumn;
QSlider *horizontalSlider;
QSpinBox *spinBoxRow;
QSlider *verticalSlider;
QPushButton *btnReset;



StructureElementSet::StructureElementSet(QDialog *dialog,QDialog *ui):SeColumns(3),SeRows(3)
{
    m_DialogUi = ui;
    m_dialog = dialog;

    connect(spinBoxColumn,SIGNAL(valueChanged(int)),this,SLOT(setColumns(int)));
    connect(horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(setColumns(int)));
    connect(spinBoxRow,SIGNAL(valueChanged(int)),this,SLOT(setRows(int)));
    connect(verticalSlider,SIGNAL(valueChanged(int)),this,SLOT(setRows(int)));

    connect(btnReset,SIGNAL(clicked()),this,SLOT(clearTable()));
    connect(tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(setCenter(int,int)));
    connect(m_dialog,SIGNAL(accepted()),this,SLOT(setSE()));
    //connect(m_DialogUi->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(add_input(int,int)));
    //connect(m_DialogUi->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(add_input(int,int)));

    connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(SeCenterX(const QString&)));
    connect(lineEdit_1,SIGNAL(textChanged(const QString&)),this,SLOT(SeCenterY(const QString&)));
}

StructureElementSet::~StructureElementSet()
{
}

void StructureElementSet::setTable()
{

    tableWidget->clearContents();
    int r = tableWidget->rowCount();
    for (int i=0;i<r;i++)
    {
        tableWidget->removeRow(0);
    }

    int c = tableWidget->columnCount();
    for (int i=0;i<c;i++)
    {
        tableWidget->removeColumn(0);
    }
    for (int i=0;i<SeRows;i++)
    {
        tableWidget->insertRow(0);
    }
    for (int i=0;i<SeColumns;i++)
    {
        tableWidget->insertColumn(0);
    }


    int r1 = tableWidget->rowCount();
    int c1 = tableWidget->columnCount();

    for (int i=0;i!= tableWidget->rowCount();i++)
    {
        for (int j=0;j!=tableWidget->columnCount();j++)
        {
            QTableWidgetItem *newItem = new QTableWidgetItem(QString::number(1));
           tableWidget->setItem(i,j,newItem);
        }
    }


    setCenter(r1/2,c1/2);

    m_dialog->update();
}

void StructureElementSet::clearTable()
{
    int r = tableWidget->rowCount();
    for (int i=0;i<=r;i++)
    {
        tableWidget->removeRow(0);
    }

    int c = tableWidget->columnCount();
    for (int i=0;i<=c;i++)
    {
        tableWidget->removeColumn(0);
    }

    SeColumns = 0;
    SeRows = 0;

    spinBoxRow->setValue(0);
    spinBoxColumn->setValue(0);
}

void StructureElementSet::setSE()
{
    m_se.clear();
    m_se.resize(SeRows);
    for (int i=0;i!=SeRows;i++)
    {
        m_se[i].resize(SeColumns,-1);
    }
//  	for (int i=0;i!=input_index.size();i++)
// 	{
// 		int r=input_index[i][0];
// 		int c=input_index[i][1];
// 		if(m_DialogUi->tableWidget->item(r,c)->text()=="")
// 			continue;
// 		m_se[r][c] = m_DialogUi->tableWidget->item(r,c)->text().toInt();
// 	}
    for (int i=0;i!=tableWidget->rowCount();i++)
    {
        for (int j=0;j!=tableWidget->columnCount();j++)
        {
            if(tableWidget->item(i,j)->text()=="")
                continue;
            m_se[i][j] = tableWidget->item(i,j)->text().toInt();
        }
    }

    //input_index.clear();

    SeCenterX = lineEdit->text().toInt()-1;
    SeCenterY = lineEdit_1->text().toInt()-1;
    emit sendSE(m_se,SeCenterX,SeCenterY);
}
